package com.yunhe.common.repository.data;

import com.yunhe.common.repository.data.impl.BaseNeo4jRepositoryImpl;
import org.neo4j.ogm.session.Session;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.support.Neo4jRepositoryFactory;
import org.springframework.data.neo4j.repository.support.Neo4jRepositoryFactoryBean;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import java.io.Serializable;

/**
 * 基础Neo4j仓库工厂
 * @author liuronglei
 */
public class BaseNeo4jRepositoryFactoryBean<R extends Neo4jRepository<T, I>, T, I extends Serializable>
        extends Neo4jRepositoryFactoryBean<R, T, I> {

    public BaseNeo4jRepositoryFactoryBean(Class<? extends R> repositoryInterface) {
        super(repositoryInterface);
    }
    @Override
    protected RepositoryFactorySupport createRepositoryFactory(Session session) {
        return new MyRepositoryFactory(session);
    }

    public static class MyRepositoryFactory<T, I extends Serializable>
            extends Neo4jRepositoryFactory {

        private final Session session;

        public MyRepositoryFactory(Session session) {
            super(session);
            this.session = session;
        }

        @Override
        protected Object getTargetRepository(RepositoryInformation information) {
            return new BaseNeo4jRepositoryImpl<T, I>((Class<T>) information.getDomainType(), session);
        }

        @Override
        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
            return BaseNeo4jRepositoryImpl.class;
        }
    }
}
